clear all 
set more off 
set maxvar 15000 
clear matrix

/*NOTE: national_percentile_outcomes.dta is included in 
        a publicly-available replication package from 
        Opportunity Insights. 
*/

*-----------------*
* MODERN DATA *
*-----------------*

use "$SupplementaryData/national_percentile_outcomes.dta" ,clear

    keep par_pctile kfr_black_female kfr_black_male kfr_white_female kfr_white_male
    keep if par_pctile==25

    rename kfr_black_female rr_group4
    rename kfr_black_male rr_group3
    rename kfr_white_male rr_group1
    rename kfr_white_female rr_group2

    reshape long rr_group, i(par_pctile) j(group)
    
    rename rr_group est_1
    gen decade=1980
    drop par_pctile
    replace est_1 = est_1*100
    
    tempfile moderndata
    save `moderndata'
 
*-----------------*
* OUR DATA 
*-----------------*

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    
    gen group=1 if race==1 & sex==1
    replace group=2 if race==1 & sex==2
    replace group=3 if race==2 & sex==1
    replace group=4 if race==2 & sex==2
    
    forval i=1(1)7 {
        gen est_`i'=. 
        gen est_lb_`i' =.
        gen est_ub_`i' =.
    }
    
    forval i=1(1)7 {
        forval j=1(1)4 {
            reg rank_son_baseline rank_father_baseline if group==`j' & decade==19`i'0 [aw=wgt_sex_race],  robust
            lincom _b[_cons]+(_b[rank_father_baseline]*25)
            
            replace est_1 = `r(estimate)' if group==`j' & decade==19`i'0    
        }
    }
    
    keep group decade est_1
    bysort group decade: keep if _n==1
    
    append using `moderndata'
    
    * Figure
    #delimit ;
    twoway (connect est_1 decade if group==1 & decade<1980, msymbol(circle) mcolor(blue)  msize(medium) lcolor(blue) lpat(dash)) 
           (connect est_1 decade if group==2 & decade<1980, msymbol(triangle) mcolor(purple) msize(medium) lcolor(purple) lpat(dash))
           (connect est_1 decade if group==3 & decade<1980, msymbol(circle) mcolor(red) msize(medium) lcolor(red) lpat(dash)) 
           (connect est_1 decade if group==4 & decade<1980, msymbol(triangle) mcolor(orange*0.7) msize(medium) lcolor(orange*0.7) lpat(dash)) 
           (scatter est_1 decade if group==1 & decade==1980, msymbol(circle) mcolor(blue)  msize(medlarge) lcolor(blue) lpat(dash)) 
           (scatter est_1 decade if group==2 & decade==1980, msymbol(triangle) mcolor(purple) msize(medlarge) lcolor(purple) lpat(dash))
           (scatter est_1 decade if group==3 & decade==1980, msymbol(circle) mcolor(red) msize(medlarge) lcolor(red) lpat(dash)) 
           (scatter est_1 decade if group==4 & decade==1980, msymbol(triangle) mcolor(orange*0.7) msize(medlarge) lcolor(orange*0.7) lpat(dash)) 
    ,
    xti(" " "Decade of respondent's birth") xlabel(1910(10)1980) xscale(range(1905 1990))
    ylabel(20(5)50) yscale(range(30 50)) ytitle("Avg. family income rank" " ")
    legend(on ring(0) row(2) pos(5) order(1 "White men" 2 "White women" 3 "Black men" 4 "Black women"))
    xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s" 1983 "1980s (IRS data)", labsize(small) )
    ;
    #delimit cr
	graph export "$Mydirectory2/main_figures_tables/figure9.pdf", as(pdf) replace
    